package com.haochang.chunkvod.controller.adapter.cash;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.haochang.base.BaseTextView;
import com.haochang.base.OnBaseClickListener;
import com.haochang.chunkvod.R;
import com.haochang.chunkvod.app.utils.CollectionUtils;
import com.haochang.chunkvod.app.utils.SaleUtils;
import com.haochang.chunkvod.model.cash.GoodInfo;
import com.haochang.chunkvod.model.cash.TakeOutInfo;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.ExpandableListView;
import android.widget.ImageView;

/**
 * @author qintao
 *         created at 2016/6/24 11:15
 *         本地购物车适配器
 */

public class TakeOutAdapter extends BaseExpandableListAdapter
{
	private Context context;
	private List<TakeOutInfo> groupList = new ArrayList<TakeOutInfo>();
	private List<List<GoodInfo>> childList = new ArrayList<List<GoodInfo>>();
	private ExpandableListView ex_listView;
	private HashMap<String, Boolean> stateHashMap = new HashMap<String, Boolean>();//用于记录每个RadioButton的状态，并保证只可选一个

	public TakeOutAdapter(Context context, ExpandableListView ex_listView, List<TakeOutInfo> groupList, HashMap<String, Boolean> stateHashMap)
	{
		this.context = context;
		this.ex_listView = ex_listView;
		this.groupList.clear();
		this.childList.clear();
		this.stateHashMap = stateHashMap;
		if (!CollectionUtils.isEmpty(groupList))
		{
			this.groupList.addAll(groupList);
			this.childList.addAll(getChildList());
		}
		TakeOutAdapter.this.notifyDataSetChanged();
	}

	public void setData(List<TakeOutInfo> groupList)
	{
		this.groupList.clear();
		this.childList.clear();
		if (!CollectionUtils.isEmpty(groupList))
		{
			this.groupList.addAll(groupList);
			this.childList.addAll(getChildList());
		}
		TakeOutAdapter.this.notifyDataSetChanged();
	}

	@Override
	public int getGroupCount()
	{
		return groupList.size();
	}

	@Override
	public int getChildrenCount(int groupPosition)
	{
		return this.childList.get(groupPosition).size();
	}

	@Override
	public Object getGroup(int groupPosition)
	{
		return groupList.get(groupPosition);
	}

	@Override
	public Object getChild(int groupPosition, int childPosition)
	{
		return childList.get(groupPosition).get(childPosition);
	}

	@Override
	public long getGroupId(int groupPosition)
	{
		return groupPosition;
	}

	@Override
	public long getChildId(int groupPosition, int childPosition)
	{
		return childPosition;
	}

	@Override
	public boolean hasStableIds()
	{
		return false;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition)
	{
		return false;
	}

	@Override
	public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent)
	{
		GroupViewHolder groupViewHolder = null;
		if (null == convertView)
		{
			convertView = LayoutInflater.from(context).inflate(R.layout.item_take_out_parent, parent, false);
			groupViewHolder = new GroupViewHolder();
			groupViewHolder.checkBox = (CheckBox) convertView.findViewById(R.id.checkBox);
			groupViewHolder.tv_serial_num = (BaseTextView) convertView.findViewById(R.id.tv_serial_num);
			groupViewHolder.tv_order_time = (BaseTextView) convertView.findViewById(R.id.tv_order_time);
			groupViewHolder.tv_totalPrice = (BaseTextView) convertView.findViewById(R.id.tv_totalPrice);
			groupViewHolder.iv_direction = (ImageView) convertView.findViewById(R.id.iv_direction);
			convertView.setTag(groupViewHolder);
		} else
			groupViewHolder = (GroupViewHolder) convertView.getTag();
		final TakeOutInfo takeOutInfo = this.groupList.get(groupPosition);
		groupViewHolder.tv_serial_num.setText(takeOutInfo.getSerialNumber());
		groupViewHolder.tv_order_time.setText(takeOutInfo.getOrderTime());
		groupViewHolder.tv_totalPrice.setText(takeOutInfo.getTotalPrice()+"");
		groupViewHolder.checkBox.setTag(groupPosition);
		groupViewHolder.checkBox.setChecked(stateHashMap.get(String.valueOf(groupPosition)));
		groupViewHolder.checkBox.setOnClickListener(mOnBaseClickListener);
		if (CollectionUtils.isEmpty(takeOutInfo.getGoodInfoList()))
			groupViewHolder.iv_direction.setVisibility(View.GONE);
		else
			groupViewHolder.iv_direction.setVisibility(View.VISIBLE);
		if (isExpanded)
			groupViewHolder.iv_direction.setImageResource(R.drawable.up);
		else
			groupViewHolder.iv_direction.setImageResource(R.drawable.down);
		groupViewHolder.iv_direction.setTag(R.id.take_out_item_id, isExpanded ? 1 : 0);
		groupViewHolder.iv_direction.setTag(groupPosition);
		groupViewHolder.iv_direction.setOnClickListener(mOnBaseClickListener);
		return convertView;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent)
	{
		ChildViewHolder childViewHolder = null;
		if (null == convertView)
		{
			convertView = LayoutInflater.from(context).inflate(R.layout.item_take_out_child, parent, false);
			childViewHolder = new ChildViewHolder();
			childViewHolder.tv_goodsName = (BaseTextView) convertView.findViewById(R.id.tv_goodsName);
			childViewHolder.tv_num_unit = (BaseTextView) convertView.findViewById(R.id.tv_num_unit);
			childViewHolder.tv_totalPrice = (BaseTextView) convertView.findViewById(R.id.tv_totalPrice);
			convertView.setTag(childViewHolder);
		} else
			childViewHolder = (ChildViewHolder) convertView.getTag();
		GoodInfo info = this.childList.get(groupPosition).get(childPosition);
		childViewHolder.tv_goodsName.setText(info.getGoodsName());
		childViewHolder.tv_num_unit.setText(String.valueOf(info.getOrderNum() + info.getUnit()));
		childViewHolder.tv_totalPrice.setText(SaleUtils.getSaleString(info.getPrice()));
		return convertView;
	}

	private OnBaseClickListener mOnBaseClickListener = new OnBaseClickListener()
	{
		@Override
		public void onBaseClick(View v)
		{
			int groupPosition = (Integer) v.getTag();
			switch (v.getId())
			{
			case R.id.iv_direction:
				int isExpanded = (Integer) v.getTag(R.id.take_out_item_id);
				if (isExpanded == 1)
					ex_listView.collapseGroup(groupPosition);
				else
					ex_listView.expandGroup(groupPosition);
				break;
			case R.id.checkBox:
				CheckBox radioButton = (CheckBox) v;
				for (String key : stateHashMap.keySet())
				{
					stateHashMap.put(key, false);
				}
				stateHashMap.put(String.valueOf(groupPosition), radioButton.isChecked());
				TakeOutAdapter.this.notifyDataSetChanged();
				break;
			default:
				break;
			}
		}
	};

	/**
	 * 获取分组下goodInfo列表
	 * @return FoodDetailInfo列表
	 */
	private List<List<GoodInfo>> getChildList()
	{
		List<List<GoodInfo>> childrenList = new ArrayList<List<GoodInfo>>();
		for (int i = 0; i < groupList.size(); i++)
		{
			TakeOutInfo takeOutInfo = groupList.get(i);
			childrenList.add(takeOutInfo.getGoodInfoList());
		}
		return childrenList;
	}

	protected class GroupViewHolder
	{
		protected CheckBox checkBox;
		protected BaseTextView tv_serial_num;
		protected BaseTextView tv_order_time;
		protected BaseTextView tv_totalPrice;
		protected ImageView iv_direction;
	}

	protected class ChildViewHolder
	{
		protected BaseTextView tv_goodsName;
		protected BaseTextView tv_num_unit;
		protected BaseTextView tv_totalPrice;
	}

}
